<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 *@param string $url: 发送请求的链接
 *@param int $httpCode: 返回的状态码
 */
function curl_get($url, &$httpCode = 0)
{
//    $url = "https://api.weixin.qq.com/sns/jscode2session?appid=wxda770323d3079560&secret=2b6f2f74625a133e96d2227e82c5782d&js_code=081SqeaD0d9zyd2EL27D0EGfaD0Sqeat&grant_type=authorization_code";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //如果不做证书校验，部署在Linux环境下请改为ture
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    $file_contents = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return json_decode($file_contents, true);
}

//获取随机32位字符1
function getRandChars($length = 0)
{
    $str = '';
    $strpoll = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789abcdefghijklmnopqrstuvwxyz';
    $maxlength = strlen($strpoll) - 1;
    for($i = 0; $i < $length; $i++)
    {
        $str .= $strpoll[rand(0, $maxlength)];
    }
    return $str;
}
//发送邮件
//邮箱配置
function phpMailer($code = '', $addressEmail = '', $title = '')
{
    //引入类文件
    import('PHPMailer5214.PHPMailerAutoload', EXTEND_PATH, '.php');
    //Create a new PHPMailer instance
    $mail = new \PHPMailer;
    $mail->IsSMTP(); // 启用SMTP
    $mail->Host = config('mail.mail_host'); //smtp服务器的名称（这里以qq邮箱为例）
    $mail->SMTPAuth = config('mail.mail_smtpauth'); //启用smtp认证
    $mail->SMTPSecure = config('mail.mail_smtpsecure'); //smtp认证方式是ssl
    $mail->Username = config('mail.mail_username'); //你的邮箱名
    $mail->Password = config('mail.mail_password'); //邮箱密码
    //Set who the message is to be sent from
    $mail->setFrom(config('mail.mail_setfrom'), 'First Last');
    $mail->FromName = config('mail.mail_fromSend'); //发件人姓名
    $mail->WordWrap = config('mail.mail_wordLength'); //设置每行字符长度
    $mail->IsHTML(config('mail.mail_ishtml')); // 是否HTML格式邮件
    $mail->CharSet = config('mail.mail_charset'); //设置邮件编码
    $mail->Body = "你需要的验证码是:<h2 style='color:red'>".$code."</h2>"; //邮件内容
    $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //邮件正文不支持HTML的备用显示
    //Set an alternative reply-to address
    $mail->addReplyTo(config('mail.mail_replyto'), 're55plyEmffail');
    //Set who the message is to be sent to
    $mail->addAddress($addressEmail, 'John Doe');
    //Set the subject line
    $mail->Subject = $title;
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    //$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
    //Replace the plain text body with one created manually
    $mail->AltBody = 'This is a plain-text message body';
    //Attach an image file
    $mail->addAttachment('/static/admin/images/phpmailer_mini.png');//附件
    //send the message, check for errors
    if (!$mail->send()) {
        return ['status' => false, 'info' => $mail->ErrorInfo];
    } else {
        session('Login:email:code', $code);
        return ['status' => true, 'info' => '验证码发送成功，请注意查收！'];
    }
}
